package com.authorityManagement.entity;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class LoginUser implements UserDetails {
      
      public User user;
      public List<Menu> menuList;
      public List<SimpleGrantedAuthority> authorities;
      
      public LoginUser(User user, List<Menu> menuList) {
            this.user = user;
            this.menuList = menuList;
      }
      
      @Override
      public Collection<? extends GrantedAuthority> getAuthorities() {
            if (!CollectionUtils.isEmpty(authorities)) {
                  return authorities;
            }
            if (!CollectionUtils.isEmpty(menuList)) {
                  authorities = menuList.stream()
                        .map(item-> new SimpleGrantedAuthority(item.getPerms()))
                        .collect(Collectors.toList());
                  return authorities;
                  
            }
            return null;
      }
      
      @Override
      public String getPassword() {
            return user.getPassword();
      }
      
      @Override
      public String getUsername() {
            return user.getUserName();
      }
      
      @Override
      public boolean isAccountNonExpired() {
            return user.getIsAccountExpired() == 0;
      }
      
      @Override
      public boolean isAccountNonLocked() {
            return user.getIsAccountLocked() == 0;
      }
      
      @Override
      public boolean isCredentialsNonExpired() {
            return user.getIsCredentialsExpired() == 0;
      }
      
      @Override
      public boolean isEnabled() {
            return user.getIsEnabled() == 0;
      }
      
      public User getUser() {
            return user;
      }
      
      public void setUser(User user) {
            this.user = user;
      }
      
      public List<Menu> getMenuList() {
            return menuList;
      }
      
      public void setMenuList(List<Menu> menuList) {
            this.menuList = menuList;
      }
}
